<?php

/**
 * index actions.
 *
 * @package    pikusa
 * @subpackage index
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class indexActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
  	$resizeW = '250';
  	$resizeH = '250';
  	
  	
    #$uploadToDirBase = "C:\AppServ\www\pikun\web\p";
    $uploadToDirBase = "/home/meteenee/domains/meteenee.com/public_html/pikun/p";
  	$webUrlBase = "http://pikun.meteenee.com";
  	
  	
  	
  	$photoTmp = $request->getFiles('Filedata');
  	/*
  	 * Example of $photoTmp;
  	 *  Array
		(
		    [name] => pol3.jpg
		    [type] => image/jpeg
		    [tmp_name] => C:\Users\obiconbig\AppData\Local\Temp\phpB78F.tmp
		    [error] => 0
		    [size] => 914086
		)
  	 */
  	
  	
  	if($photoTmp[error]){
  		// return upload error;
  	}
  	
  	
  	
  	$originalPath = $uploadToDirBase."/o";
  	$thumbnailPath = $uploadToDirBase."/t";
  	
  	$imageDatabasePath = "/p/o";
  	$originalWebPath = $webUrlBase."/p/o";
  	$thumbnailWebPath = $webUrlBase."/p/t";
  	$viewWebPath = $webUrlBase."/v/p";
  	
  	
  	
  	
  	$imageName = time().rand(100,999);
  	
  	$imagesize = getimagesize($photoTmp['tmp_name']);
  	
  	switch($imagesize['mime']){
  		case "image/jpeg" :
  			$imageExtension = ".jpg";
  			break;
  		case "image/pjpeg" :
  			$imageExtension = ".jpg";
  			break;
  		case "image/png" :
  			$imageExtension = ".png";
  			break;
  		case "image/x-png" :
  			$imageExtension = ".png";
  			break;
  		case "image/gif" :
  			$imageExtension = ".gif";
  			break;
  	}
  	$imageName = $imageName.$imageExtension;
  	
  	
  	
  	
  	
  	
  	$imagePath = $originalPath."/".$imageName;
  	$imageThumbnailPath = $thumbnailPath."/".$imageName;
  	
  	
  	$imageDatabasePath .= "/".$imageName;
  	$imagUrl = $originalWebPath."/".$imageName;
  	$imagThumbnailUrl = $thumbnailWebPath."/".$imageName;
  	$viewUrl = $viewWebPath."/".$imageName;
  	
  	
  	if(move_uploaded_file($photoTmp['tmp_name'], $imagePath)){
  		
  		$photo = new Photo();
  		$photo->setName($imageName);
	  	$photo->setUrl($imageDatabasePath);
	  	$photo->setIspicasa(false);
	  	$photo->setCreatedAt(date('Y-m-d H:i:s'));
	  	
	  	$photo->save();
	  	
		//Create the thumbnail
	  	$OriImagePath = $imagePath;
	  	$ThuImagePath = $imageThumbnailPath;
	  	$MaxHeight = $resizeH;
	  	$MaxWidth = $resizeW;
	  	$Quality = 100;
	  	$this->Resize($OriImagePath,$ThuImagePath,$MaxWidth,$MaxHeight,$Quality);
	  	
	  	
	  	$output = array(
		  	'original' => $imagUrl,
		  	'thumb' => $imagThumbnailUrl,
	  		'view' => $viewUrl,
	  		'fulllink' => htmlentities('<a href="'.$viewUrl.'"><img src="'.$imagUrl.'" /></a>'),
	  		'thumblink' => htmlentities('<a href="'.$viewUrl.'"><img src="'.$imagThumbnailUrl.'" /></a>'),
	  		'bbfull' => htmlentities('[url='.$viewUrl.'][img]'.$imagUrl.'[/img][/url]'),
	  		'bbthumb' => htmlentities('[url='.$viewUrl.'][img]'.$imagThumbnailUrl.'[/img][/url]'),
	  	);
	  	
	  	$this->getResponse()->setHttpHeader('Content-Type','application/json; charset=utf-8');
	  	return $this->renderText(json_encode($output));
	  	
  	}else{
  		$this->redirect404();
  	}
  	
  	return sfView::NONE;
  }
  
  
  
	function Resize($OriImagePath,$ThuImagePath,$MaxWidth,$MaxHeight,$Quality) {
	  list($ImageWidth,$ImageHeight,$TypeCode)=getimagesize($OriImagePath);
	  $ImageType=($TypeCode==1?"gif":($TypeCode==2?"jpeg":
	             ($TypeCode==3?"png":FALSE)));
	  $CreateFunction="imagecreatefrom".$ImageType;
	  $OutputFunction="image".$ImageType;
	  if ($ImageType) {
	    $Ratio=($ImageHeight/$ImageWidth);
	    $ImageSource=$CreateFunction($OriImagePath);
	    if ($ImageWidth > $MaxWidth || $ImageHeight > $MaxHeight) {
	      if ($ImageWidth > $MaxWidth) {
	         $ResizedWidth=$MaxWidth;
	         $ResizedHeight=$ResizedWidth*$Ratio;
	      }
	      else {
	        $ResizedWidth=$ImageWidth;
	        $ResizedHeight=$ImageHeight;
	      }       
	      if ($ResizedHeight > $MaxHeight) {
	        $ResizedHeight=$MaxHeight;
	        $ResizedWidth=$ResizedHeight/$Ratio;
	      }      
	      $ResizedImage=imagecreatetruecolor($ResizedWidth,$ResizedHeight);
	      imagecopyresampled($ResizedImage,$ImageSource,0,0,0,0,$ResizedWidth,
	                         $ResizedHeight,$ImageWidth,$ImageHeight);
	    }
	    else {
	      $ResizedWidth=$ImageWidth;
	      $ResizedHeight=$ImageHeight;     
	      $ResizedImage=$ImageSource;
	    }   
	    $OutputFunction($ResizedImage,$ThuImagePath,$Quality);
	    return true;
	  }   
	  else
	    return false;
	}
}